local MedBay = GS:addState('MedBay')

function MedBay:enterState()
	clearLoveCallbacks()
	print("entering medbay state")

	local lm = map[2]

	

	map_items = {}

	local mGrid = Grid(lm.layout)
	local mFinder = Pathfinder(mGrid, 'JPS', med_floor)
	mFinder:setMode('ORTHOGONAL')

	


	if lm.visited == false then

		map_x = lm.map_x
		map_y = lm.map_y

		pX = map_x + view_center_x
		pY = map_y + view_center_y

		local mtpts = m_get_exits(lm)

		for i = 1, #mtpts do

			Elevator:new(mtpts[i].x, mtpts[i].y, mtpts[i].dest)

		end

		local tablet = Tablet:new("tablet 002", tablet_img,
			12, 5, 0, tablet_text[2])


		lm.visited = true
		player.x = lm.features.start.x*tile_size
		player.y = lm.features.start.y*tile_size

		Fog:multi(lm.layout)

		Item:spawn_items(lm.items)

		BloodSplatter:place_random(40, lm.layout, med_floor) -- do this last of all features 
		--to avoid placing over them

		local msg_opt = {
			name = player.name,
		}

		msgs[#msgs+1] = _navi:new('You\'ve entered the Medical Bay', msg_opt)

		if game_options.moody == true then

		else
			light_scene()
		end

	else

		map_items = MedBay.map_items
		features = MedBay.features
		events = MedBay.events
		enemies = MedBay.enemies 
		fog = MedBay.fog
		map_x = MedBay.last_map_x
		map_y = MedBay.last_map_y

		pX = map_x + view_center_x
		pY = map_y + view_center_y

	end

	

	draw_map(lm.layout)

	con_count = 0
	light_count = 0
	m_count = player.fire_rate*60
	function love.update(dt)

		arc.check_keys(dt)


		global_updates(dt)

		p_update(dt)
		update_animations(dt, anims)


		light_features()

		Event:update(dt)
		Enemy:update(dt)
		Item:update(dt)
		FX:update_all(dt)

		update_proj(dt, projectiles, lm.layout)
		mouse_update(dt)

		con_count = update_con(con_count)
		light_count = p_light_energy(light_count)



	end

	function love.draw()

		love.graphics.setColor(66,66,66)
		love.graphics.draw(tileset_batch)
		Event:draw_all()
		love.graphics.setColor(255,255,255)

		--light_tiles(lm.layout)


		draw_group(features)

		draw_proj(projectiles)
		draw_animations(anims)

		Item:draw_all()

		draw_player(player)

		Enemy:draw_all()

		Fog:draw()

		FX:draw_all()

		draw_hud()

		msg_queue_draw() -- must be before arc.clear_key()

		arc.clear_key()



		draw_mouse()

	end



	function love.keypressed(key, unicode)

		arc.set_key(key)

		p_control(key,unicode, lm)

	end

	function love.mousepressed(x, y, button)

		mouse_click(x, y, button)

	end

	function m_splatter_callback()

		--draw_text('all splatters cleaned')
		--map_fade.fade = true
		local tablet = Feature:get_by_name( "tablet 002" )

		local tx = tablet.x_index
		local ty = tablet.y_index
		local tr = tablet.rotation

		features[tablet.y_index][tablet.x_index] = nil

		local tablet = Tablet:new("tablet 002", 
			tablet_img, tx, ty, tr, "Medical Analyzer is now available.")

		local ma = MedAnalyzer:new(9, 7)



	end

end

function MedBay:exitState()

	player.last_state = 'MedBay'

	print("exiting medbay state")

	MedBay.map_items = map_items
	MedBay.features = features
	MedBay.events = events
	MedBay.enemies = enemies
	MedBay.fog = fog
	MedBay.last_map_x = map_x
	MedBay.last_map_y = map_y

	map_items = {}
	features = {}
	events = {}
	enemies = {}
	fog = {}


	clean_msg()

end